package io.lkt.modules.sys.service;

import io.lkt.common.utils.DateUtil;
import io.lkt.modules.sys.entity.BBeginnerArticleTypeEntity;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.Query;

import io.lkt.modules.sys.dao.BPlatformBeginnerArticleTypeDao;
import io.lkt.modules.sys.entity.BPlatformBeginnerArticleTypeEntity;
import io.lkt.modules.sys.service.BPlatformBeginnerArticleTypeService;
import org.springframework.transaction.annotation.Transactional;


@Service("bPlatformBeginnerArticleTypeService")
public class BPlatformBeginnerArticleTypeServiceImpl extends ServiceImpl<BPlatformBeginnerArticleTypeDao, BPlatformBeginnerArticleTypeEntity> implements BPlatformBeginnerArticleTypeService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BPlatformBeginnerArticleTypeEntity> page = this.page(
                new Query<BPlatformBeginnerArticleTypeEntity>().getPage(params),
                new QueryWrapper<BPlatformBeginnerArticleTypeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<String> getGzhCodeList(Integer articleTypeId) {
        return baseMapper.getGzhCodeList(articleTypeId);
    }

    @Override
    @Transactional
    public void updateObject(Integer articleTypeId, List<String> gzhCodeList) {
        if (CollectionUtils.isNotEmpty(gzhCodeList)) {
            // 1、删除不在集合中记录
            baseMapper.deletePlatformBeginnerArticleType(articleTypeId, gzhCodeList);

            //2、遍历集合
            gzhCodeList.forEach((gzhCode) -> {
                BPlatformBeginnerArticleTypeEntity entity = baseMapper.getPlatformBeginnerArticleType(articleTypeId, gzhCode);
                if (Objects.isNull(entity)) {
                    // 如果不存在，则新增
                    entity = new BPlatformBeginnerArticleTypeEntity();
                    entity.setArticleTypeId(articleTypeId);
                    entity.setGzhCode(gzhCode);
                    entity.setSort(Integer.valueOf(1));
                    entity.setCreateTime(DateUtil.getCurDate());

                    baseMapper.saveObject(entity);
                }
            });
        }
    }

    @Override
    public List<BPlatformBeginnerArticleTypeEntity> queryList(Map<String, Object> params) {
        return baseMapper.queryList(params);
    }

    @Override
    public int queryListTotal(Map<String, Object> params) {
        return baseMapper.queryListTotal(params);
    }

    @Override
    public BPlatformBeginnerArticleTypeEntity getInfo(String gzhCode, Integer articleTypeId) {
        return baseMapper.getInfo(gzhCode, articleTypeId);
    }

    @Override
    public List<BBeginnerArticleTypeEntity> getArticleTypeByGzhCode(String gzhCode) {
        return baseMapper.getArticleTypeByGzhCode(gzhCode);
    }

    @Override
    public int updateByInfo(BPlatformBeginnerArticleTypeEntity bPlatformBeginnerArticleType) {
        return baseMapper.updateByInfo(bPlatformBeginnerArticleType);
    }

}
